package com.tacc.tyun.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-15 9:41
 **/
public class BaseServiceImpl<T> implements BaseService<T> {

    protected BaseDao<T> baseDao;

    @Override
    public T selectByPrimaryKey(Object key) {
        return this.baseDao.selectByPrimaryKey(key);
    }

    @Override
    public List<T> select(T t) {
        return this.baseDao.select(t);
    }

    @Override
    public List<T> selectAll() {
        return this.baseDao.selectAll();
    }

    @Override
    public int delete(T t) {
        return this.baseDao.delete(t);
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return this.baseDao.deleteByPrimaryKey(key);
    }

    @Override
    public int deleteByIds(String ids) {
        return this.baseDao.deleteByIds(ids);
    }

    @Override
    public int insert(T t) {
        return this.baseDao.insert(t);
    }

    @Override
    public T selectOne(T t) {
        return this.baseDao.selectOne(t);
    }

    @Override
    public int updateByPrimaryKey(T t) {
        return this.baseDao.updateByPrimaryKey(t);
    }

    @Override
    public int updateByPrimaryKeySelective(T t) {
        return this.baseDao.updateByPrimaryKeySelective(t);
    }


    @Override
    public int selectCount(T t) {
        return this.baseDao.selectCount(t);
    }

    @Override
    public PageInfo listForPage(T t, int pageNum, int pageSize) {

        Assert.notNull(t);

        if (pageNum <= 0) {
            pageNum = BaseConstant.PAGE_NUM;
        }

        if (pageSize <= 0) {
            pageSize = BaseConstant.PAGE_SIZE;
        }

        PageHelper.startPage(pageNum, pageSize);
        //分页查询
        Example example = new Example(t.getClass());
        return new PageInfo(baseDao.selectByExample(example));
    }

    /**
     * 根据多个主键查询多条记录
     * @param clazz
     * @param property
     * @param values
     * @return
     */
    public List<T> selectByIds(Class<T> clazz, String property, List<Object> values) {
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, values);
        return this.baseDao.selectByExample(example);
    }

    /**
     * 批量修改，t中是修改的参数，如t.setRemovetag(1);可以批量软删除
     * @param clazz
     * @param property
     * @param values
     * @param t
     * @return
     */
    public int updateByIds(Class<T> clazz, String property, List<Object> values, T t) {
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, values);
        return this.baseDao.updateByExampleSelective(t,example);
    }

}
